from sqlalchemy import BigInteger, Column, DateTime, Integer, String
from sqlalchemy.schema import FetchedValue
from flask_sqlalchemy import SQLAlchemy
from common.models.Customer import Customer
from common.models.Shop import Shop
from application import db


class ShopCoupon(db.Model):
    __tablename__ = 'shop_coupon'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False, server_default=db.FetchedValue())
    start_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    end_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    time_type = db.Column(db.Integer, nullable=False)
    full_cash = db.Column(db.Numeric(10, 2), nullable=False)
    minus_cash = db.Column(db.Numeric(10, 2), nullable=False)
    cash_type = db.Column(db.Integer, nullable=False)
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    shop_id = db.Column(db.ForeignKey('food_db.shop.id'), nullable=False, index=True)

    shop = db.relationship('Shop', primaryjoin='ShopCoupon.shop_id == Shop.id', backref='shop_coupons')


class CustomerCoupon(db.Model):
    __tablename__ = 'customer_coupon'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    c_id = db.Column(db.ForeignKey('food_db.customer.id'), nullable=False, index=True)
    sc_id = db.Column(db.ForeignKey('food_db.shop_coupon.id'), nullable=False, index=True)
    is_used = db.Column(db.Integer, nullable=False)
    is_valid = db.Column(db.Integer, nullable=False)
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())

    c = db.relationship('Customer', primaryjoin='CustomerCoupon.c_id == Customer.id', backref='customer_coupons')
    sc = db.relationship('ShopCoupon', primaryjoin='CustomerCoupon.sc_id == ShopCoupon.id', backref='customer_coupons')
